<template>
    <div class="page project-set">
        <div class="weui-cells" v-if='true'>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">项目名称</label></div>
                <div class="weui-cell__bd">
                    <input v-if="isUpdateTitle" v-model="item.name" class="weui-input" type="text" placeholder="请输入" value="{{item.name}}" maxlength="20">
                    <p v-if="!isUpdateTitle">{{item.name}}</p>
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">项目描述</label></div>
                <div class="weui-cell__bd">
                    <input v-if="isUpdateDesc" v-model="item.msg" class="weui-input" type="text" placeholder="请输入" maxlength="40">
                    <p v-if="!isUpdateDesc">{{item.msg}}</p>
                </div>
            </div>
        </div>
        <div class="weui-cells" v-if="isUpdateTitle">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectStartTime()">
                <div class="weui-cell__bd">
                    <p><span class="title">开始时间</span>{{item.start_date}}</p>
                </div>
                <div class="weui-cell__ft"></div>
            </a>
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectEndTime()">
                <div class="weui-cell__bd">
                    <p><span class="title">结束时间</span>{{item.end_date}}</p>
                </div>
                <div class="weui-cell__ft"></div>
            </a>
        </div>
        <div class="weui-cells" v-if="!isUpdateTitle">
            <a class="weui-cell weui-cell_access" href="javascript:;">
                <div class="weui-cell__bd">
                    <p><span class="title">开始时间</span>{{item.start_date}}</p>
                </div>
            </a>
            <a class="weui-cell weui-cell_access" href="javascript:;">
                <div class="weui-cell__bd">
                    <p><span class="title">结束时间</span>{{item.end_date}}</p>
                </div>
            </a>
        </div>
        <div class="weui-cells">
            <div class="weui-cell">
                <div class="weui-cell__bd">
                    <p><span class="title">项目所属</span>{{item.team_name}}</p>
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__bd">
                    <p><span class="title">项目类型</span>{{item.project_name}}</p>
                </div>
            </div>
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectCity()" v-if="isUpdateDesc">
                <div class="weui-cell__bd">
                    <p><span class="title">项目城市</span><span v-if="item.city_name" :value="item.city_name">{{item.city_name}}</span><span v-else class="placeholder">请选择</span></p>
                </div>
                <div class="weui-cell__ft"></div>
            </a>
            <a class="weui-cell" href="javascript:;" v-else>
                <div class="weui-cell__bd">
                    <p><span class="title">项目城市</span>{{item.city_name}}</p>
                </div>
            </a>
        </div>
        <div class="weui-cells">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectRelateTeam" v-if="item.relate_team_can_edit == 1">
                <div class="weui-cell__bd">
                    <p><span class="title">合作企业</span><span v-if="item.relate_team_name" :value="item.relate_team_name">{{item.relate_team_name}}</span><span v-else class="placeholder">选填</span></p>
                </div>
                <div class="weui-cell__ft"></div>
            </a>
            <a class="weui-cell" href="javascript:;" v-else>
                <div class="weui-cell__bd">
                    <p><span class="title">合作企业</span>{{item.relate_team_name}}</p>
                </div>
            </a>
        </div>
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell weui-cell_switch">
                <div class="weui-cell__bd"><label class="weui-label">置顶项目</label></div>
                <div class="weui-cell__ft">
                    <input :checked="isSetTop" @click="setTop" class="weui-switch" type="checkbox">
                </div>
            </div>
            <!--<div class="weui-cell weui-cell_switch">
                <div class="weui-cell__bd"><label class="weui-label">消息免打扰</label></div>
                <div class="weui-cell__ft">
                    <input :checked="isMessageSet" @click="setSilent" class="weui-switch" type="checkbox">
                </div>
            </div>-->
        </div>
        <div class="weui-cells">
            <!-- 去掉项目昵称入口 -->
            <!-- <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">项目昵称</label></div>
                <div class="weui-cell__bd">
                    <input v-model="item.nick_name" class="weui-input" type="text" placeholder="" value="{{item.nick_name}}" maxlength="20">
                </div>
            </div> -->
            <a class="weui-cell weui-cell_access" href="dmsaas://SelectSinglePeople" v-show="isChangeRoot">
                <div class="weui-cell__bd" >
                    <p><span class="title">转让超级管理员</span></p>
                </div>
                <div class="weui-cell__ft">
                </div>
            </a>
        </div>  
        <div class="btn-group com-border-top">
            <a @click="exitPro(teamId,proId)" v-show="!isChangeRoot && (!isDepartment)" class="com-border-bottom touch">退出项目</a>
            <a @click="endUpPro(teamId,proId,isProOpen)" v-show="isChangeRoot&&isProOpen==3" class="com-border-bottom touch">结束项目</a>
            <a @click="startUpPro(teamId,proId,isProOpen)" v-show="isChangeRoot&&isProOpen==1" class="com-border-bottom touch">开启项目</a>
            <a @click="deletePro" v-show="isDeletePro" class="com-border-bottom touch btn_delete">删除项目</a>
        </div>
    </div>
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import $ from 'zepto'
    import * as util from 'js/util.js'

    import userCenterApi from 'clientApi/userCenterApi'
    import commonPage from 'clientApi/commonPage'

    import clientInfo from 'gapi/clientInfo'
    import clientUi from 'gapi/clientUi'
    import widget from 'gapi/widget'
    import cache from 'gapi/cache'
    import KCApiApp from 'clientApi/KCApiApp.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                userId: '',
                roleId: 1,//fixme
                teamId: '',
                proId: '',
                item: {},
                permissionData: {},
                isProOpen:3,
                oName: '',
                oNickName: '',
                oDesc: '' ,
                isChange: [],
                oStart_date:'',
                oEnd_date:'' ,          
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        computed: {
            isSave: function(){
                let oStr = oName + oNickName + oDesc
                let nStr = this.item.name + this.item.nick_name + this.item.msg
                if(oStr == nStr){
                    return true
                }else{
                    return false
                }
            },
            isMessageSet: function(){
                return !!this.item.message_type;
            },
            isSetTop: function(){
                return !!this.item.top;
            },
            //是否有修改项目名称的权限
            isUpdateTitle: function(){
                return this.permissionData.data.some((o, i, a) => {
                    if(o.id_name == "update_project_name" || o.desc == "修改项目名称"){
                        return true
                    }
                })
            },
            //是否有修改项目描述的权限
            isUpdateDesc: function(){
                return this.permissionData.data.some((o, i, a) => {
                    if(o.id_name == "update_project_msg" || o.desc == "修改项目描述"){
                        return true
                    }
                })
            },
            //是否有更改超级管理员的权限
            isChangeRoot: function(){
                return this.permissionData.data.some((o, i, a) => {
                    if(o.id_name == "change_project_name" || o.desc == "转让项目超级管理员"){
                        return true
                    }
                })
            },
            //是否有结束项目的权限
            isUpdate: function(){
                return this.permissionData.data.some((o, i, a) => {
                    if(o.id_name == "stop_project" || o.desc == "结束项目"){
                        return true
                    }
                })
            },
            //是否有删除项目的权限
            isDeletePro: function(){
                return this.permissionData.data.some((o, i, a) => {
                    if(o.id_name == "delete_project" || o.desc == "删除项目"){
                        return true
                    }
                })
            },
            //是否为部门负责人
            isDepartment: function(){
                return this.permissionData.data.some((o, i, a) => {
                    if(o.id_name == "invisible_man" || o.desc == "当前用户不在项目中"){
                        return true
                    }
                })
            }
            
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){

                this.proId = util.getQueryString('project_id') || util.getUrddataString('project_id')//project id
                this.teamId = util.getQueryString('team_id') || util.getUrddataString('team_id')
              
                this.getPermission(this.teamId, this.proId)
                this.getProInfo(this.teamId, this.proId)
                
                commonPage.getWebViewID((data)=>{
                    cache.setMemoryCache('webViewID',{'webViewID':data})
                })
            },
            selectStartTime(){
                var self = this;
                let date = new Date()
                let currentDate = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()
                let maxDate = this.item.end_date?this.item.end_date:'3000-12-12'
                KCApiApp.showDatePicker(
                        currentDate,//  最小时间
                        maxDate,//  最大时间
                        self.item.start_date, //默认时间
                        (data)=>{    
                            if(data.SelectedDate){
                                self.updateProjectTime(data.SelectedDate,self.item.end_date)
                            }
                        })
            },
            selectEndTime(){
                var self = this;
                let date = new Date()
                let currentDate = this.item.start_date
                let maxDate = (date.getFullYear()+1)+"-"+(date.getMonth()+1)+"-"+date.getDate()
                KCApiApp.showDatePicker(
                        currentDate,//  最小时间
                        maxDate,//  最大时间
                        this.item.end_date, //默认时间
                        (data)=>{                  //回调方法
                            if(data.SelectedDate){
                                self.updateProjectTime(self.item.start_date,data.SelectedDate)
                            }
                        })
            },
            selectRelateTeam() {
                // alert(this.teamId+ ' ' +this.proId)
                window.location.href="dmsaas://CooperateEnterprise?urdData=" + encodeURIComponent(`team_id=${this.teamId}&project_id=${this.proId}&relate_team_id=${this.item.relate_team_id == 0 ? '' : this.item.relate_team_id}&relate_team_name=${this.item.relate_team_name}&source=old`)
            },
            updateProjectTime (start_time,end_time){
                var self = this;
                console.log({
                        team_id: self.teamId, 
                        project_id: self.proId,
                        start_date:start_time,
                        end_date:end_time
                    })
                util.ajax({
                    url: '/project/info/update_period',
                    type:'GET',
                    data: {
                        team_id: self.teamId, 
                        project_id: self.proId,
                        start_date:start_time,
                        end_date:end_time
                    },
                    success: (res) => {
                        console.log(res)
                        if(res&&res.errno==0){
                            widget.toast('修改成功')
                            self.item.start_date = start_time
                            self.item.end_date = end_time
                        }else{
                            widget.toast('数据获取失败')
                        }
                    },
                    error: (xhr, error) => {
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            setTop (teamId,proId){
                this.item.top = !this.item.top
                let _action = this.item.top ? 1 : 0; //1-置顶，0-取消置顶
                commonPage.loadPageStatus('loading');
                util.ajax({
                    url: '/project/top/set',
                    type: 'POST',
                    data: {
                        team_id: this.teamId, //fixme this.teamId
                        project_id: this.proId,//fixme:this.projectId
                        action: _action
                    },
                    success: (result) => {
                        if(result.errno == 0){
                            if(_action == 1){
                                //埋点
                                let statisticsLog_data = {
                                    event_id: '项目设置页-置顶项目',
                                    // event_label: data.event_label,(预留，现在不用传此参数)
                                    event_param: {
                                        ca_from: 'top',
                                        project_id: this.proId,
                                    }
                                }
                                // alert(JSON.stringify(statisticsLog_data))
                                KCApiApp.statisticsLog(statisticsLog_data)

                            }else{
                                //埋点
                                let statisticsLog_data = {
                                    event_id: '项目设置页-取消置顶项目',
                                    // event_label: data.event_label,(预留，现在不用传此参数)
                                    event_param: {
                                        ca_from: 'canceltop',
                                        project_id: this.proId,
                                    }
                                }
                                // alert(JSON.stringify(statisticsLog_data))
                                KCApiApp.statisticsLog(statisticsLog_data)
                            }
                         
                        }else{
                            widget.toast(result.errmsg)
                        }
                        commonPage.loadPageStatus('loadSuccess');
                    },
                    error: (xhr, error) => {
                        let err=JSON.parse(xhr.responseText);
                        widget.toast(err.message);

                        commonPage.loadPageStatus('loadSuccess');
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络');  
                        commonPage.loadPageStatus('loadSuccess');
                    }
                })
            },
            setSilent (teamId,proId){
                this.item.message_type = !this.item.message_type
                let _action = this.item.message_type ? 1 : 0; //1-免打扰，0-取消免打扰
                commonPage.loadPageStatus('loading');
                util.ajax({
                    url: '/project/message/set',
                    type: 'POST',
                    data: {
                        team_id: this.teamId, //fixme this.teamId
                        project_id: this.proId,//fixme:this.projectId
                        action: _action
                    },
                    success: (result) => {
                        if(result.errno == 0){
                            
                        }else if(result.errno == 15011){
                            widget.toast('项目已经结束')
                            this.item.message_type = 0
                        }else{
                            widget.toast(result.errmsg)
                        }
                        commonPage.loadPageStatus('loadSuccess');
                    },
                    error: (xhr, error) => {
                        let err=JSON.parse(xhr.responseText);
                        widget.toast(err.message);

                        commonPage.loadPageStatus('loadSuccess');
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络');  
                        commonPage.loadPageStatus('loadSuccess');
                    }
                })
            },
            getProInfo (teamId, proId){
                const self = this;
                commonPage.loadPageStatus('loading');
                util.ajax({
                    url: '/project/info/get',
                    type: 'GET',
                    data: {
                        team_id: teamId, //fixme this.teamId
                        project_id: proId,//fixme:this.projectId
                    },
                    success: (result) => {
                        // alert(JSON.stringify(result.data))
                        commonPage.loadPageStatus('loadSuccess');
                        if(result.errno == 0){
                            self.item = result.data
                            self.oName = self.item.name
                            self.oNickName = self.item.nick_name
                            self.oDesc = self.item.msg
                            self.isProOpen = self.item.status == 3 ? 1 : 3
                            self.oStart_date = self.item.start_date
                            self.oEnd_date = self.item.end_date
                            self.oCity_name = self.item.city_name
                            self.oCity_id = self.item.city_id
                            // self.isChange.push(self.item.name)
                            // self.isChange.push(self.item.nick_name)
                            // self.isChange.push(self.item.msg)
                            
                        }else{
                            commonPage.loadPageStatus('loadSuccess');
                            widget.toast(result.errmsg)
                        }
                    },
                    error: (xhr, error) => {
                        commonPage.loadPageStatus('loadSuccess');
                        let err=JSON.parse(xhr.responseText);
                        widget.toast(err.message);
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess');
                        widget.toast('网络连接失败，请检查网络');  
                    }
                })
            },
            getPermission (teamId, proId){
                util.ajax({
                    url: '/permission/application',
                    type: 'GET',
                    data: {
                        team_id: this.teamId, //fixme this.teamId
                        project_id: this.proId,//fixme:this.projectId
                        application_id: 0,
                    },
                    success: (result) => {
                        if(result.errno == 0){
                            this.permissionData = result;
                            if(this.isChangeRoot){
                                this.roleId = 0;
                            }
                            
                        }else{
                            widget.toast(result.errmsg)
                        }
                    },
                    error: (xhr, error) => {
                        let err=JSON.parse(xhr.responseText);
                        widget.toast(err.message);
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络');  
                    }
                })
            },
            exitPro (teamId,proId){
                clientUi.showDialog('退出项目', '从项目中退出后将无法看到任何项目相关信息, 是否确认?', function(data){
                    if(data.status==1){
                        util.ajax({
                            url: '/project/user/quit',
                            type: 'POST',
                            data: {
                                team_id: teamId, //fixme this.teamId
                                project_id: proId,//fixme:this.projectId
                            },
                            success: (result) => {
                                if(result.errno == 0){
                                    widget.toast('成功退出项目');
                                        cache.getMemoryCache('jcIndexWebViewID', (data) => {
                                            if(data.webViewID){
                                                commonPage.goBack(1,{},data.webViewID)
                                            }
                                    }) 
                                }else{
                                    widget.toast(result.errmsg)
                                }
                            },
                            error: (xhr, error) => {
                                let err=JSON.parse(xhr.responseText);
                                widget.toast(err.message);
                            },
                            noNetwork: () => {
                                widget.toast('网络连接失败，请检查网络');  
                            }
                        })
                    }
                }, '确定', '取消')
            },
            endUpPro (teamId,proId,isProOpen){
                const self = this;
                clientUi.showDialog('结束项目', '你可以将项目结束进行归档,结束的项目可以很容易再次开启,并且不会有数据丢失', function(data){
                    if(data.status==1){
                        util.ajax({
                            url: '/project/status/action',
                            type: 'POST',
                            data: {
                                team_id: teamId, //fixme this.teamId
                                project_id: proId,//fixme:this.projectId
                                action: isProOpen
                            },
                            success: (result) => {

                                if(result.errno == 0){
                                    //埋点
                                    let statisticsLog_data = {
                                        event_id: '项目设置页-结束项目',
                                        // event_label: data.event_label,(预留，现在不用传此参数)
                                        event_param: {
                                            ca_from: 'end',
                                            project_id: proId,
                                        }
                                    }
                                    // alert(JSON.stringify(statisticsLog_data))
                                    KCApiApp.statisticsLog(statisticsLog_data)

                                    self.isProOpen = 1;
                                    widget.toast('成功结束项目');
                                    cache.getMemoryCache('jcIndexWebViewID', (data) => {
                                        if(data.webViewID){
                                            commonPage.goBack(1,{},data.webViewID)
                                        }
                                    })
                                }else{
                                    widget.toast(result.errmsg);
                                }
                            },
                            error: (xhr, error) => {
                                let err=JSON.parse(xhr.responseText);
                                widget.toast(err.message);
                            },
                            noNetwork: () => {
                                widget.toast('网络连接失败，请检查网络');  
                            }
                        })
                    }
                }, '确定', '取消')
            },
            //开启项目
            startUpPro (teamId,proId,isProOpen){

                const self = this;
                let date = new Date()
                let y = date.getFullYear()
                let m = date.getMonth() < 9 ? '0' + (date.getMonth()+1) : (date.getMonth()+1)
                let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
                let today = y + '-' + m + '-' + d
                console.log(today)
                console.log(this.item.end_date)
                if(this.item.end_date < today){
                    widget.toast('项目结束时间必须大于或等于当前时间')
                    return
                }
                if(this.item.start_date > this.item.end_date){
                    widget.toast('项目结束时间不能小于开始时间')
                    return
                }
                
                util.ajax({
                    url: '/project/status/action',
                    type: 'POST',
                    data: {
                        team_id: teamId, //fixme this.teamId
                        project_id: proId,//fixme:this.projectId
                        action: isProOpen
                    },
                    success: (result) => {
                        if(result.errno == 0){
                            self.isProOpen = 3;
                            widget.toast('成功开启')
                            cache.getMemoryCache('jcIndexWebViewID', (data) => {
                                if(data.webViewID){
                                    commonPage.goBack(1,{},data.webViewID)
                                }
                            })
                        }else{
                            widget.toast(result.errmsg)
                        }
                    },
                    error: (xhr, error) => {
                        let err=JSON.parse(xhr.responseText);
                        widget.toast(err.message);
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络');
                    }
                })
                /*clientUi.showDialog('开启项目', '你可以将项目结束进行归档,结束的项目可以很容易再次开启,并且不会有数据丢失', function(data){
                    if(data.status==1){

                    }
                }, '确定', '取消')*/
                //  window.location.href="dmsaas://ProjectList?"//进行中的项目列表
            },
            deletePro (teamId,proId){
                clientUi.showDialog('删除项目', '一旦你删除了项目,所有与项目有关的信息将被永久删除!', (data) => {
                    if(data.status == 1){
                        if(this.isChangeRoot){//confirm
                            commonPage.loadPageStatus('loading');
                            util.ajax({
                                url: '/project/info/delete',
                                type: 'POST',
                                data: {
                                    team_id: this.teamId, //fixme this.teamId
                                    project_id: this.proId
                                },
                                success: (result) => {
                                    if(result.errno == 0){
                                        //埋点
                                        let statisticsLog_data = {
                                            event_id: '项目设置页-删除项目',
                                            // event_label: data.event_label,(预留，现在不用传此参数)
                                            event_param: {
                                                ca_from: 'delete',
                                                project_id: this.proId,
                                            }
                                        }
                                        // alert(JSON.stringify(statisticsLog_data))
                                        KCApiApp.statisticsLog(statisticsLog_data)

                                       widget.toast("成功删除")
                                       cache.getMemoryCache('jcIndexWebViewID', (data) => {
                                            if(data.webViewID){
                                               commonPage.goBack(1,{},data.webViewID)
                                           }
                                        })
                                    }else{
                                        widget.toast(result.errmsg)
                                    }
                                    commonPage.loadPageStatus('loadSuccess');
                                },
                                error: (xhr, error) => {
                                    let err=JSON.parse(xhr.responseText);
                                    widget.toast(err.message);
                                    commonPage.loadPageStatus('loadSuccess');
                                },
                                noNetwork: () => {
                                    widget.toast('网络连接失败，请检查网络');  
                                    commonPage.loadPageStatus('loadSuccess');
                                }
                            })
                        }
                    }
                }, '确定', '取消')
            },
            //保存
            doSave (){
                //校验
                const self = this;
                let bName = false;
                let bNickname = false;
                let bDesc = false;
                let bStart_date = false;
                let bEnd_date = false
                let bCity_name = false

                //判断项目名称是否有改动或者为空  true表示空或者未改动
                if(self.item.name == ''){
                    widget.toast('请填写项目名称');
                    bName = true;
                }
                if(self.item.city_name == ''){
                    widget.toast('请填写项目城市');
                    bCity_name = true;
                }
                // if(self.item.start_date == ''){
                //     widget.toast('请选择开始时间');
                //     bStart_date = true;
                // }
                // if(self.item.end_date == ''){
                //     widget.toast('请选择结束时间');
                //     bEnd_date = true;
                // }
                // //项目昵称判断同上
                // if(self.item.nick_name == ''){
                //     widget.toast('请填写昵称');
                //     bNickName = true;
                // }
                // //项目描述判断同上
                // if(self.item.msg == ''){
                //     widget.toast('请填写项目描述');
                //     bDesc = true;
                // }

                if (bName || bNickname || bDesc || bCity_name){
                    return false
                }else{
                    let saveUrl = '';
                    let saveDate = {};
                    if(self.isUpdateTitle && self.isUpdateDesc){
                        saveUrl = '/project/info/update';
                        saveDate = {
                            team_id: self.teamId, 
                            project_id: self.proId,
                            name: self.item.name,
                            msg: self.item.msg,
                            start_date:self.item.start_date,
                            end_date:self.item.end_date,
                            city_name:self.item.city_name,
                            city_id:self.item.city_id
                        }
                        util.ajax({
                            url: '/project/info/updateusername',
                            type: 'POST',
                            data: {
                                team_id: self.teamId, 
                                project_id: self.proId,
                                nick_name: self.item.nick_name
                            },
                            success: (result) => {
                                console.log(result)
                                if(result.errno == 0){
                                   
                                }else{
                                    widget.toast(result.errmsg)
                                }
                            },
                            error: (xhr, error) => {
                                let err=JSON.parse(xhr.responseText);
                                widget.toast(err.message);
                            },
                            noNetwork: () => {
                                widget.toast('网络连接失败，请检查网络');  
                            }
                        })
                    }else{
                        saveUrl = '/project/info/updateusername';
                        saveDate = {
                            team_id: self.teamId, 
                            project_id: self.proId,
                            nick_name: self.item.nick_name,
                        }
                    }
                    commonPage.loadPageStatus('loading')
                    util.ajax({
                        url: saveUrl,
                        type: 'POST',
                        data: saveDate,
                        success: (result) => {
                            console.log(result)
                            if(result.errno == 0){
                                commonPage.loadPageStatus('loadSuccess')
                                widget.toast('成功保存')
                                commonPage.goBack(1, {projectName : saveDate.name})
                            }else{
                                widget.toast(result.errmsg)
                                commonPage.loadPageStatus('loadSuccess')
                            }
                        },
                        error: (xhr, error) => {
                            let err=JSON.parse(xhr.responseText);
                            widget.toast(err.message);
                            commonPage.loadPageStatus('loadSuccess');
                        },
                        noNetwork: () => {
                            widget.toast('网络连接失败，请检查网络');  
                            commonPage.loadPageStatus('loadSuccess');
                        }
                    })
                }

            },
            //保存项目城市
            citySave (){    
                //校验
                const self = this;
                let bName = false;
                let bNickname = false;
                let bDesc = false;
                let bStart_date = false;
                let bEnd_date = false
                let bCity_name = false

                if (bName || bNickname || bDesc || bCity_name){
                    return false
                }else{
                    let saveUrl = '';
                    let saveDate = {};
                    if(self.isUpdateTitle && self.isUpdateDesc){
                        saveUrl = '/project/info/update';
                        saveDate = {
                            team_id: self.teamId, 
                            project_id: self.proId,
                            name: self.item.name,
                            msg: self.item.msg,
                            start_date:self.item.start_date,
                            end_date:self.item.end_date,
                            city_name:self.item.city_name,
                            city_id:self.item.city_id
                        }
                        util.ajax({
                            url: '/project/info/updateusername',
                            type: 'POST',
                            data: {
                                team_id: self.teamId, 
                                project_id: self.proId,
                                nick_name: self.item.nick_name
                            },
                            success: (result) => {
                                console.log(result)
                                if(result.errno == 0){
                                   
                                }else{
                                    widget.toast(result.errmsg)
                                }
                            },
                            error: (xhr, error) => {
                                let err=JSON.parse(xhr.responseText);
                                widget.toast(err.message);
                            },
                            noNetwork: () => {
                                widget.toast('网络连接失败，请检查网络');  
                            }
                        })
                    }else{
                        saveUrl = '/project/info/updateusername';
                        saveDate = {
                            team_id: self.teamId, 
                            project_id: self.proId,
                            nick_name: self.item.nick_name,
                        }
                    }
                    commonPage.loadPageStatus('loading')
                    util.ajax({
                        url: saveUrl,
                        type: 'POST',
                        data: saveDate,
                        success: (result) => {
                            console.log(result)
                            if(result.errno == 0){
                                commonPage.loadPageStatus('loadSuccess')
                                widget.toast('修改成功')
                            }else{
                                widget.toast(result.errmsg)
                                commonPage.loadPageStatus('loadSuccess')
                            }
                        },
                        error: (xhr, error) => {
                            let err=JSON.parse(xhr.responseText);
                            widget.toast(err.message);
                            commonPage.loadPageStatus('loadSuccess');
                        },
                        noNetwork: () => {
                            widget.toast('网络连接失败，请检查网络');  
                            commonPage.loadPageStatus('loadSuccess');
                        }
                    })
                }

            },
            //选择城市
            selectCity (){
                //增加选中状态时使用
                let encodeStr = `city_id=${this.item.city_id}&city_name=` + encodeURIComponent(`${this.item.city_name}`)
                window.location.href = `dmsaas://SelectCityList?${encodeStr}`
            }
        },
        created(){
            this.init()
            const self = this
            window.doSave = self.doSave
            // window.loadData = this.setRoot;
            window.loadData = (data) => {
                console.log("loadData123"+JSON.stringify(data))
                console.log(data.value.id || data.value.user_id)
                if((data.value.id || data.value.user_id) !== undefined){
                    commonPage.loadPageStatus('loading')
                    util.ajax({
                        url: '/project/supuser/change',
                        type: 'POST',
                        data: {
                            project_id:self.proId,
                            team_id:self.teamId,
                            supuser_id:data.value.id || data.value.user_id
                        },
                        success: (result) => {
                            console.log(result)
                            if(result.errno == 0){
                                widget.toast('转让成功');
                                self.getPermission(self.teamId, self.proId)
                                self.getProInfo(self.teamId, self.proId)
                            }else if(result.errno == 15025){
                                widget.toast('无法转让给自己')
                            }else{
                                widget.toast(result.errmsg);
                            }
                        },
                        error: (xhr, error) => {
                            let err=JSON.parse(xhr.responseText);
                            widget.toast(err.message);
                        },
                        noNetwork: () => {
                            widget.toast('网络连接失败，请检查网络');  
                        }
                    })
                    commonPage.loadPageStatus('loadSuccess')
                }else{                    
                    if(data){
                        let key = data.key
                        let value = data.value
                        switch(key){ 
                                //更新城市信息
                            case 'city_data':
                                this.item.city_name = value.city_name
                                this.item.city_id = value.city_id
                                this.citySave();
                                break
                            case 'relate_team_info':
                                // alert(JSON.stringify(value))
                                this.item.relate_team_id = value.team_id ? value.team_id : ''
                                this.item.relate_team_name = value.team_name ? value.team_name : ''
                                break
                            default:
                                break
                        }
                    }
                }
               
            }
        },
        ready(){
            this.$watch('item',(newValue,oldValue) => {
                let bChange = newValue.name == this.oName&&
                              newValue.nick_name == this.oNickName&&
                              newValue.msg == this.oDesc
                              // &&
                              // newValue.start_date == this.oStart_date&&
                              // newValue.end_date == this.oEnd_date;
                if(!bChange){
                    commonPage.updateTitleBar({
                        backAction: 'gotoback',
                        leftDisplay: false,
                        leftText: '',
                        leftAction: 'gotoback',
                        rightDisplay: true,
                        rightText: '保存',
                        rightAction: 'doSave'
                    })
                }else{
                    commonPage.updateTitleBar({
                        backAction: 'gotoback',
                        leftDisplay: false,
                        leftText: '',
                        leftAction: 'gotoback',
                        rightDisplay: false,
                        rightText: '保存',
                        rightAction: 'doSave'
                    })
                }
            },{deep:true})
            window.gotoback = () => {
                const self = this;
                if(self.item.city_name == '' && self.isUpdateDesc){
                    widget.toast('请选择项目城市');
                    return
                }else{
                    commonPage.goBack(1)
                }
            }
        }

    }


</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/project.css"></style>

